#!/bin/bash

######################################################################
#
#  Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
#  --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
passfile='/etc/nginx/.htpasswd'

if [ "$(id -u)" != "0" ]; then
  echo "Must be run as root, or with sudo"
  exit 1
fi

confirm_name=1
while [ $confirm_name = 1 ]
  do
    read -p "Enter user to delete: " answer
    user=$answer
    check_name=1
    while [ $check_name = 1 ]
      do
        echo "Warning if you continue you will permanently delete the user and all their data"
        read -p "Is $user correct, y to continue, n to exit? " answer
        case $answer in [Yy]* ) confirm_name=0 && check_name=0  ;;
                        [Nn]* ) exit  ;;
                            * ) echo "Enter y or n";;
        esac
    done
done

if [ "$user" = "$SUDO_USER" ]; then
echo "Cannot delete yourself" && exit
elif [ "$user" = "root" ]; then
echo "Cannot delete root" && exit
elif [ -z "$user" ]; then
echo "User entry is blank" && exit
elif ! id -u $user >/dev/null 2>&1 ; then
echo "$user does not exist" && exit
fi

echo
echo "Deleting $user"

passwd -l $user
killall -KILL -u $user
crontab -u $user -r
if [ -d /var/www/rutorrent/conf/users/$user ]; then
rm -r /var/www/rutorrent/conf/users/$user
fi
htpasswd -D $passfile $user
deluser --remove-home $user
rm -r /var/run/screen/S-$user
ssh_allow=$(grep "$user" /etc/ssh/sshd_config)
if ! [ -z "$ssh_allow" ]; then
sed -i "s/ $user//g" /etc/ssh/sshd_config
service ssh restart
fi
